/**
 * 对象数组按属性去重
 * @param {Array} arr - 对象数组
 * @param {String|Function} key - 属性名或生成key的函数
 * @returns {Array} 去重后的新数组
 */
function uniqueBy(arr, key) {
  if (!Array.isArray(arr)) {
    throw new TypeError('Expected an array');
  }
  
  const seen = new Map();
  return arr.filter(item => {
    const k = typeof key === 'function' 
      ? key(item)
      : item[key];
    
    return seen.has(k) ? false : seen.set(k, true);
  });
}

// 使用示例
const users = [
  {id: 1, name: 'John'},
  {id: 2, name: 'Jane'},
  {id: 1, name: 'Johnny'}
];

console.log(uniqueBy(users, 'id'));
// [{id:1,name:'John'}, {id:2,name:'Jane'}]

console.log(uniqueBy(users, user => user.name.toLowerCase()));
// 按name小写去重